package com.yuyou.tas.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.admin.service.IAuthOrganService;
import com.yuyou.tas.admin.service.ILoginService;
import com.yuyou.tas.admin.vo.req.ReqLogin;
import com.yuyou.tas.admin.vo.req.ReqPassWordReset;
import com.yuyou.tas.admin.vo.req.ReqPassWordUpdate;
import com.yuyou.tas.admin.vo.resp.RespLogin;
import com.yuyou.tas.service.api.admin.entity.AuthOrganizationExt;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;

@Api(value="AuthResourceController",description="登录密码接口")
@Validated
@RestController
@RequestMapping(value="/api/")
public class LoginController extends BaseController{

	@Autowired
	private ILoginService loginService;
	
	@Autowired
	private IAuthOrganService authOrganService;
	
	@ApiOperation(value="登录")
	@RequestMapping(value="/login" ,method=RequestMethod.POST)
	public ReturnResult<RespLogin> login(HttpServletRequest request,@Validated @ModelAttribute ReqLogin reqLogin){
		ResBoolSimpleInfo<RespLogin> res=loginService.login(reqLogin);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());
	}
	
	@ApiOperation(value="获取账号可用机构列表")
	@RequestMapping(value="/login/findActiveOrganByAcc" ,method=RequestMethod.POST)
	public ReturnResult<List<AuthOrganizationExt>> findActiveOrganByAcc(HttpServletRequest request,
			@NotBlank(message="phone不能为空") String phone){	
		List<AuthOrganizationExt> list=authOrganService.findActiveOrganByAcc(phone);
		return ReturnResult.success(list);	
	}
	
	@ApiOperation(value="修改密码")
	@RequestMapping(value="/updatePass" ,method=RequestMethod.POST)
	public ReturnResult<String> updatePass(HttpServletRequest request,@Validated @ModelAttribute ReqPassWordUpdate reqPassWordUpdate){	
		LoginRedisObj user = this.getUserObj(request);
		ResBoolSimpleInfo<String> res=loginService.updatePass(user.getUserId(),reqPassWordUpdate);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());		

	}
	
	@ApiOperation(value="重置密码")
	@RequestMapping(value="/resetPass" ,method=RequestMethod.POST)
	public ReturnResult<String> resetPass(HttpServletRequest request,@Validated @ModelAttribute ReqPassWordReset reqPassWordReset){	
		LoginRedisObj user = this.getUserObj(request);
		ResBoolSimpleInfo<String> res=loginService.resetPass(user.getUserId(),reqPassWordReset);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());		

	}
	
	
	

}
